var config = require('../../utils/config');
var util = require('../../utils/util');
Page({
  data:{
    id:0,
    userbookId:0,
    jxkey:'',
    allbookList:[],
    alltitleList:[],
    allidList:[],
    allqList:[],
    bookList:[],
    allquestionList:[],
    questionList:[],
    allquestionCList:[],
    questionCList:[],
    selected:true,
    selected1:false,
    selected2:false,
    showimg:0,
    showimg1:0,
    optionList:[],
    hideFlag: true,//true-隐藏 false-显示
    animationData: {},
    scrollId:'',
    priorPage:0,
    page:0,
    qpriorPage: 0,
    qpage: 0,
    scrollleft:false,
    scrollright:false,
    showAll:false,
    qscrollId:'',
    qscrollleft:false,
    qscrollright:false,
    qshowAll:false,
    hideFlagR: true,//true-隐藏 false-显示
    animationDataR: {},
    optionListR:[],
    page: 1,
    dropDownMenuTitle: ['全部'],
    grades:[{id:'', title:'全部'},{id:'0', title:'未掌握'},{id:'1', title:'已掌握'}],
    curGrade:'',
    curGradeName:''
  },
  onLoad:function(options) {
    this.queryBookContent(options.id);
    this.setData({
      id:options.id,
      userbookId:options.userbookId,
      jxkey:options.jxkey
    })
  },
  selected:function(e) {
    var num = parseInt(this.data.page);
    this.setData({
      selected:true,
      selected1:false,
      selected2:false,
      bookList: [this.data.allbookList[num-1],this.data.allbookList[num],this.data.allbookList[num+1]],
      showAll:false,
      scrollleft:false,
      scrollright:true,
      scrollId:'p1',
      priorPage:num-1,
      page:num
    })
  },
  selected1:function(e) {
    var num = parseInt(this.data.page);
    this.setData({
      selected:false,
      selected1:true,
      selected2:false,
      bookList: [this.data.allbookList[num-1],this.data.allbookList[num],this.data.allbookList[num+1]],
      showAll:false,
      scrollleft:false,
      scrollright:true,
      scrollId:'p1',
      priorPage:num-1,
      page:num
    })
  },
  selected2:function(e) {
    this.setData({
      selected:false,
      selected1:false,
      selected2:true,
      curGrade:'0',
      curGradeName:'未掌握'
    })
    let dropdowm = this.selectComponent('#dropdownmenu');    
    dropdowm.showInitGrade('0','未掌握');
  },
  selectGradeItem:function(e) {
    this.setData({
      curGrade: e.detail.selectedId,
      curGradeName:e.detail.selectedTitle
    });
    let userInfo = wx.getStorageSync('user');
    this.queryErrorQ(this.data.id,userInfo.userId,e.detail.selectedId);
  },
  queryBookContent:function(id) {
    var that = this;
    var options = {
      url: config.clubApi.getDetail,
      data: {
        id: id
      }
    };
    util.request(options, function(res) {
      if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
        let allList = [];
        let allTList = [];
        let allIDList = [];
        let allqList = [];
        res.data.data.forEach((item)=>{
          allList.push(item.url);
          allTList.push(item.title);
          allIDList.push(item.id);
          allqList.push({id:item.id,qlist:item.detailList});
        })
        that.setData({
          allbookList: allList,
          alltitleList: allTList,
          allidList: allIDList,
          allqList: allqList,
          bookList: [allList[0],allList[1]],
          scrollId: 'p0',
          page:0
        })
      }
    });
  },
  queryErrorQ:function(id, userId, status) {
    var that = this;
    var options = {
      url: config.clubApi.getErrorQ,
      data: {
        taskId: id,
        userId: userId,
        status: status
      }
    };
    util.request(options, function(res) {
      if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
        let allqlist = [];
        let allqclist = [];
        res.data.data.forEach((item)=>{
          allqlist.push(item.qfileUrl);
          allqclist.push('第'+item.pageNum+'页第'+item.questionNum+'题：'+item.jxcontent);
        })
        if (allqlist.length>=2) {
          that.setData({
            qpage:0,
            allquestionList:allqlist,
            questionList:[allqlist[0],allqlist[1]],
            allquestionCList:allqclist,
            questionCList:[allqclist[0],allqclist[1]]            
          })
        }else {
          that.setData({
            qpage:0,
            allquestionList:allqlist,
            questionList:[allqlist[0]],
            allquestionCList:allqclist,
            questionCList:[allqclist[0]]            
          })
        }
      }
    });
  },
  pagingU() {
    if (this.data.scrollright==true) {
      if (this.data.page==1&&this.data.priorPage<this.data.page) {
        this.setData({
          bookList: [this.data.allbookList[0],this.data.allbookList[1]],
          priorPage:0,
          page:0
        })
      }else {
        this.setData({
          priorPage:this.data.page,
          page:this.data.page-1
        })
      }
    }else if (this.data.page==1) {
      this.setData({
        priorPage:2,
        page:1
      })
    }
    this.setData({
      showAll:false,
      scrollleft:true,
      scrollright:false
    })
    if(this.data.scrollleft==true&&this.data.page>=1&&this.data.priorPage>=this.data.page) {
      let curPage = this.data.page;
      if (this.data.allbookList[curPage-2]==undefined) {
        this.setData({
          bookList: [this.data.allbookList[curPage-1],this.data.allbookList[curPage]],
          scrollId: 'p1',
          priorPage:curPage,
          page:curPage-1
        })
      }else {
        this.setData({
          bookList: [this.data.allbookList[curPage-1],this.data.allbookList[curPage],this.data.allbookList[curPage+1]],
          scrollId: 'p1',
          priorPage:curPage,
          page:curPage-1
        })
      }
    }
  },
  pagingD() {
    if (this.data.scrollleft==true) {
      if (this.data.page>0) {
        this.setData({
          priorPage:this.data.page,
          page:this.data.page+1
        })
      }else {
        this.setData({
          priorPage:0,
          page:0
        })
      }
    }
    this.setData({
      showAll:false,
      scrollleft:false,
      scrollright:true
    })
    if(this.data.scrollright==true&&this.data.page>=0&&this.data.page<=this.data.allbookList.length-2&&this.data.priorPage<=this.data.page) {
      let curPage = this.data.page;
      if (this.data.allbookList[curPage+2]==undefined) {
        this.setData({
          bookList: [this.data.allbookList[curPage],this.data.allbookList[curPage+1]],
          scrollId: 'p1',
          priorPage:curPage,
          page:curPage+1
        })
      }else {
        this.setData({
          bookList: [this.data.allbookList[curPage],this.data.allbookList[curPage+1],this.data.allbookList[curPage+2]],
          scrollId: 'p1',
          priorPage:curPage,
          page:curPage+1
        })
      }
    }
  },
  pagingUQ() {
    if (this.data.qscrollright==true) {
      if (this.data.qpage==1&&this.data.qpriorPage<this.data.qpage) {
        this.setData({
          questionList:[this.data.allquestionList[0],this.data.allquestionList[1]],
          questionCList:[this.data.allquestionCList[0],this.data.allquestionCList[1]],
          qpriorPage:0,
          qpage:0
        })
      }else {
        this.setData({
          qpriorPage:this.data.qpage,
          qpage:this.data.qpage-1
        })
      }
    }else if (this.data.qpage==1) {
      this.setData({
        qpriorPage:2,
        qpage:1
      })
    }
    this.setData({
      qshowAll:false,
      qscrollleft:true,
      qscrollright:false
    })
    if(this.data.qscrollleft==true&&this.data.qpage>=1&&this.data.qpriorPage>=this.data.qpage) {
      let curPage = this.data.qpage;
      if (this.data.allquestionList[curPage-2]==undefined) {
        this.setData({
          questionList:[this.data.allquestionList[curPage-1],this.data.allquestionList[curPage]],
          questionCList:[this.data.allquestionCList[curPage-1],this.data.allquestionCList[curPage]],
          qscrollId:'q1',
          qpriorPage:curPage,
          qpage:curPage-1
        })
      }else {
        this.setData({
          questionList:[this.data.allquestionList[curPage-1],this.data.allquestionList[curPage],this.data.allquestionList[curPage+1]],
          questionCList:[this.data.allquestionCList[curPage-1],this.data.allquestionCList[curPage],this.data.allquestionCList[curPage+1]],
          qscrollId:'q1',
          qpriorPage:curPage,
          qpage:curPage-1
        })
      }
    }
  },
  pagingDQ() {
    if (this.data.qscrollleft==true) {
      if (this.data.qpage>0) {
        this.setData({
          qpriorPage:this.data.qpage,
          qpage:this.data.qpage+1
        })
      }else {
        this.setData({
          qpriorPage:0,
          qpage:0
        })
      }
    }
    this.setData({
      qshowAll:false,
      qscrollleft:false,
      qscrollright:true
    })
    if(this.data.qscrollright==true&&this.data.qpage>=0&&this.data.qpage<=this.data.allquestionList.length-2&&this.data.qpriorPage<=this.data.qpage) {
      let curPage = this.data.qpage;
      if (this.data.allquestionList[curPage+2]==undefined) {
        this.setData({
          questionList:[this.data.allquestionList[curPage],this.data.allquestionList[curPage+1]],
          questionCList:[this.data.allquestionCList[curPage],this.data.allquestionCList[curPage+1]],
          qscrollId:'q1',
          qpriorPage:curPage,
          qpage:curPage+1
        })
      }else {
        this.setData({
          questionList:[this.data.allquestionList[curPage],this.data.allquestionList[curPage+1],this.data.allquestionList[curPage+2]],
          questionCList:[this.data.allquestionCList[curPage],this.data.allquestionCList[curPage+1],this.data.allquestionCList[curPage+2]],
          qscrollId:'q1',
          qpriorPage:curPage,
          qpage:curPage+1
        })
      }
    }
  },
  previewImg:function(e) {
    let index = this.data.allbookList.indexOf(this.data.bookList[e.target.dataset.index]);
    var img = this.data.allbookList;
    wx.previewImage({
      current: img[index],
      urls: img,
    })
  },
  previewImg1:function(e) {    
    let page = this.data.page;
    let question = this.data.allqList[page];
    if (question!=undefined&&question.qlist&&question.qlist.length>0) {
      wx.setStorageSync('qlist'+question.id, question.qlist);
      wx.navigateTo({
        url: '../bookQuestion/bookQuestion?userbookId='+this.data.userbookId+
          '&contentId='+this.data.allidList[page]+
          '&key='+this.data.alltitleList[page]+
          '&page='+page
      });
    }else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '没有必要拍照，没有需要检查的题',
        success: function (res) {}
      })
    }
  },
  /*getPhone:function() {
    let curPage = this.data.page<=0?1:(this.data.scrollright?this.data.page+1:this.data.page+2);
    if (this.data.alltitleList[curPage-1]!=null) {
      let page = curPage-1;
      let question = this.data.allqList[page];
      if (question.qlist&&question.qlist.length>0) {
        wx.setStorageSync('qlist'+question.id, question.qlist);
        wx.navigateTo({
          url: '../head/head?userbookId='+this.data.userbookId+
            '&contentId='+this.data.allidList[page]+
            '&key='+this.data.alltitleList[page]+
            '&page='+page
        });
      }else {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '没有必要拍照，没有需要检查的题',
          success: function (res) {}
        })
      }
    }
    wx.navigateTo({
      url: "../camera/camera?userbookId="+this.data.userbookId
    });
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'], //从相册选择
      success: function (res) {
        res.tempFilePaths.forEach((item)=>{
          that.onUploadFile(item)
        })
      },
    });
  },*/
  onUploadFile(pathStr){
    let _this = this;
    wx.uploadFile({
      url: config.clubApi.saveWorkPhone,
      method: 'POST',
      filePath: pathStr,
      name: 'file',
      formData: {
        'id': _this.data.userbookId
      },
      success (res){
        if (res.statusCode===200) {
          util.showSuccess('录入成功!', config.showSuccessTime, () => {
            wx.navigateBack();
          })
        }
      }
    })
  },
  //取消
  mCancel:function() {
    var that = this;
    that.hideModal();
  },
  mCancelR:function() {
    var that = this;
    that.hideModalR();
  },
  // 显示遮罩层
  getProcess:function() {
    var that = this;
    var options = {
      url: config.clubApi.getDetailJx,
      data: {
        id: this.data.id,
        jxkey: this.data.jxkey
      }
    };
    util.request(options, function(res) {
      if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
        var titles = [];
        for (var i = 0; i < res.data.data.length; i++) {
          var subChild = [];
          if (res.data.data[i].subTitleList) {
            var subList = res.data.data[i].subTitleList;
            for (var j = 0; j < subList.length; j++) {
              subChild.push({
                title:subList[j].title,
                pageNum:subList[j].pageNum,
                children:[]
              });
            }
          }
          titles.push({
            title:res.data.data[i].title,
            pageNum:res.data.data[i].pageNum,
            children:subChild
          });
        }
        that.setData({
          optionList:titles,
          showAll:true
        })
      }
    });
    that.setData({
      hideFlag: false
    })
    // 创建动画实例
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间
      timingFunction: 'ease',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation = animation; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideIn();//调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 100)
  },
  getCurProcess:function() {
    var that = this;
    var options = {
      url: config.clubApi.getDetailJx,
      data: {
        id: this.data.id,
        jxkey: this.data.jxkey
      }
    };
    util.request(options, function(res) {
      if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
        var titles = [];
        for (var i = 0; i < res.data.data.length; i++) {
          var subChild = [];
          if (res.data.data[i].subTitleList) {
            var subList = res.data.data[i].subTitleList;
            for (var j = 0; j < subList.length; j++) {
              subChild.push({
                title:subList[j].title,
                pageNum:subList[j].pageNum,
                children:[]
              });
            }
          }
          titles.push({
            title:res.data.data[i].title,
            pageNum:res.data.data[i].pageNum,
            children:subChild
          });
        }
        that.setData({
          optionListR:titles,
          showAll:true
        })
      }
    });
    that.setData({
      hideFlagR: false
    })
    // 创建动画实例
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间
      timingFunction: 'ease',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation = animation; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideInR();//调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 100)
  },
  // 隐藏遮罩层
  hideModal:function() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间 默认400ms
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation;
    that.slideDown();//调用动画--滑出
    var time1 = setTimeout(function () {
      that.setData({
        hideFlag: true        
      })
      clearTimeout(time1);
      time1 = null;
    }, 220)//先执行下滑动画，再隐藏模块  
  },
  hideModalR:function() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间 默认400ms
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation;
    that.slideDownR();//调用动画--滑出
    var time1 = setTimeout(function () {
      that.setData({
        hideFlagR: true        
      })
      clearTimeout(time1);
      time1 = null;
    }, 220)//先执行下滑动画，再隐藏模块  
  },
  //动画 -- 滑入
  slideIn:function() {
    this.animation.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: this.animation.export()
    })
  },
  //动画 -- 滑出
  slideDown:function() {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
  tapItem: function(e) {
    var that = this;
    var num = parseInt(e.detail.itemid);
    that.hideModal();
    that.setData({
      bookList: [this.data.allbookList[num-1],this.data.allbookList[num],this.data.allbookList[num+1]],
      showAll:false,
      scrollleft:false,
      scrollright:true,
      scrollId:'p1',
      priorPage:num-1,
      page:num
    });
  },
  //动画 -- 滑入
  slideInR:function() {
    this.animation.translateY(0).translateX(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationDataR: this.animation.export()
    })
  },
  //动画 -- 滑出
  slideDownR:function() {
    this.animation.translateY(0).translateX(300).step()
    this.setData({
      animationDataR: this.animation.export(),
    })
  },
  tapItemR: function(e) {
    var that = this;
    var num = parseInt(e.target.id);    
    that.hideModalR();
    that.setData({
      bookList: [this.data.allbookList[num-1],this.data.allbookList[num],this.data.allbookList[num+1]],
      showAll:false,
      scrollleft:false,
      scrollright:true,
      scrollId:'p1',
      priorPage:num-1,
      page:num
    });
  }
})